package com.example.myhuangye;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myhuangye.call.Contacts;
import com.example.myhuangye.call.Dialog;
import com.example.myhuangye.call.SelectTel;

import java.util.List;

public class Main3Activity extends AppCompatActivity implements Dialog.getEditTexts {
    private ListView listView;
    private List<Contacts> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        listView= (ListView) findViewById(R.id.listView_main);
        list= SelectTel.select(this);
        listView.setAdapter(new MyAdapter());
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Contacts contacts=list.get(i);
                Intent intent=new Intent();
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:"+contacts._tel));
                startActivity(intent);
            }//调用拨号服务
        });
        findViewById(R.id.HT).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void texts(String name, String pass) {
        Toast.makeText(this, name+""+pass, Toast.LENGTH_SHORT).show();
    }

    class MyAdapter extends BaseAdapter{
                                //适配器
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View itemView=null;
            if(view==null){
                itemView=View.inflate(Main3Activity.this,R.layout.item,null);
            }else {
                itemView=view;
            }
            TextView textView= (TextView) itemView.findViewById(R.id.tv_item)
                    ,tt = (TextView) itemView.findViewById(R.id.dianhuahaoma)
                    ,tt1 = (TextView) itemView.findViewById(R.id.dianhuaxingming);;
            try {
                Contacts contacts=list.get(i);
                tt.setText("电话:"+contacts._tel);
                tt1.setText("姓名:"+contacts._name);
                textView.setText(contacts._name.substring(0,1)+"");//获取电话姓名的第一个字符显示
            }catch (NullPointerException e){
            }
            return itemView;
        }
    }
}
